package com.neouton.crm.enterprise.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neouton.common.util.PageUtil;
import com.neouton.crm.enterprise.entity.EnterpriseVO;
import com.neouton.crm.enterprise.mapper.EnterpriseMapper;
import com.neouton.crm.enterprise.service.IEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements IEnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    /**
     * 多条件分页查询企业信息列表
     * @Timestamp: 2022-11-1
     * @Author: LiBingKun
     * @param page 分页信息
     * @param enterprise 其余查询条件
     * @return 分页查询结果
     */
    public PageUtil<EnterpriseVO> queryEnterpriseByPage(IPage page, EnterpriseVO enterprise){
        IPage<EnterpriseVO> iPage = enterpriseMapper.queryEnterpriseByPage(page,enterprise);
        return new PageUtil(iPage);
    }

    /**
     * 获取所有工商信息接口，对外提供
     * @param source
     * @return
     */
    public List<EnterpriseVO> queryEnterprises(String source){
        return enterpriseMapper.queryEnterprises(source);
    }

    /**
     * 保存或更新工商信息
     * @Timestamp: 2022-11-3
     * @Author: LiBingKun
     * @param enterprise 需更新实体
     * @return 成功失败
     */
    public Integer saveOrUpdateEnterprises(EnterpriseVO enterprise){
        int enterpriseId;
        if(null == enterprise.getEnterpriseId()){
            Boolean status = enterpriseMapper.insertEnterprises(enterprise);
            if (status){
                enterpriseId = enterprise.getEnterpriseId();
            }else {
                enterpriseId = 0;
            }
            return enterpriseId;
        }else {
            enterpriseMapper.backEnterprisesData(enterprise.getLastUpdateBy(),enterprise.getEnterpriseId());
            Boolean status = enterpriseMapper.updateEnterprises(enterprise);
            if (status){
                enterpriseId = enterprise.getEnterpriseId();
            }else{
                enterpriseId = 0;
            }
            return enterpriseId;
        }
    }

    /**
     * 删除工商信息
     * @Timestamp: 2022-11-3
     * @Author: LiBingKun
     * @param enterpriseId 需删除的ID
     * @return 成功失败
     */
    public Boolean deleteEnterprisesById(Integer enterpriseId,Integer backUserId){
        enterpriseMapper.backEnterprisesData(backUserId,enterpriseId);
        return enterpriseMapper.deleteEnterprisesById(enterpriseId);
    }

    /**
     * 更具部分条件查询工商信息是否存在
     * @Timestamp: 2022-11-3
     * @Author: LiBingKun
     * @param enterprise 条件封装实体类
     * @return true存在，false不存在
     */
    public Boolean queryEnterpriseExit(EnterpriseVO enterprise){
        return enterpriseMapper.queryEnterpriseExit(enterprise);
    }

}
